﻿/******************************************************    
文件名:GetSteelname.cs
版权  (c)  2010-11-23 淘渺圈圈工作室
作者:黄媛媛
创建日期:2010-12-07
维护人员:黄媛媛
维护日期:2010-12-07
文件功能描述:金属材料热处理网络学习平台_访问牌号相关数据表操作
******************************************************/
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Web;
using Mmht.DB;
using System.Configuration;
using System.Collections;

namespace Mmht.BT
{
    public class GetSteelname
    {
        TableSteelnameDB ts = new TableSteelnameDB();
        GetQuestion gq = new GetQuestion();
        int close_number = 8;//单选题总数目

        private DataTable getAllSteelname()
        {
            return ts.getSteelname();
        }

        private DataTable creatTable()
        {
            DataTable table = new DataTable();
            table.Columns.Add("number");
            table.Columns.Add("id");
            table.Columns.Add("question");
            table.Columns.Add("answer");
            table.Columns.Add("answer1");
            table.Columns.Add("A1");
            table.Columns.Add("B1");
            table.Columns.Add("C1");
            table.Columns.Add("D1");
            table.Columns.Add("answer2");
            table.Columns.Add("A2");
            table.Columns.Add("B2");
            table.Columns.Add("C2");
            table.Columns.Add("D2");
            table.Columns.Add("answer3");
            table.Columns.Add("A3");
            table.Columns.Add("B3");
            table.Columns.Add("C3");
            table.Columns.Add("D3");
            table.Columns.Add("answer4");
            table.Columns.Add("A4");
            table.Columns.Add("B4");
            table.Columns.Add("C4");
            table.Columns.Add("D4");
            table.Columns.Add("answer5");
            table.Columns.Add("A5");
            table.Columns.Add("B5");
            table.Columns.Add("C5");
            table.Columns.Add("D5");
            return table;
        }
        //8道完型填空
        public DataTable getClose()
        {
            DataTable dt = new DataTable();
            dt = getAllSteelname();

            DataTable table = creatTable();

            int max = dt.Rows.Count;
            List<int> list = new List<int>();
            list = gq.getQuestionID(max, close_number);

            List<DataRow> qaRowList = this.getQAList(dt, list);

            string id = string.Empty;
            string question = string.Empty;
            string answer = string.Empty;
            Random rd = new Random();
            for (int i = 0; i < list.Count; i++)
            {
                string answers = string.Empty;
                DataRow row = table.NewRow();
                DataRow qaRow = qaRowList[i];                
                //row 添加 number
                int rowNumber = i + 1;
                row["number"] = rowNumber.ToString();
                //row 添加 id
                id = "cloze_" + i.ToString();
                row["id"] = id;
                //row 添加 问题
                row["question"] = qaRow["steel_name"].ToString();
                //row 添加 第1题
                answer = qaRow["steel_type"].ToString();
                if (answer == null)
                {
                    answer = "无";
                }
                else if (answer == "")
                {
                    answer = "无";
                }
                row["answer1"] = answer;
                if (answers.Contains("$"))
                {
                    answers.Replace("$", "#");
                }
                answers += answer + "$";
                List<string> list1 = this.getSelect1(answer);
                row["A1"] = list1[0];
                row["B1"] = list1[1];
                row["C1"] = list1[2];
                row["D1"] = list1[3];
                //row 添加 第2题
                answer = qaRow["heat_treating_technics"].ToString();
                if (answer == null)
                {
                    answer = "无";
                }
                else if (answer == "")
                {
                    answer = "无";
                }
                row["answer2"] = answer;
                if (answers.Contains("$"))
                {
                    answers.Replace("$", "#");
                }
                answers += answer + "$";
                List<string> list2 = this.getSelect2(answer);
                row["A2"] = list2[0];
                row["B2"] = list2[1];
                row["C2"] = list2[2];
                row["D2"] = list2[3];
                //row 添加 第3题
                answer = qaRow["structure"].ToString();
                if (answer == null)
                {
                    answer = "无";
                }
                else if (answer == "")
                {
                    answer = "无";
                }
                row["answer3"] = answer;
                if (answers.Contains("$"))
                {
                    answers.Replace("$", "#");
                }
                answers += answer + "$";
                List<string> list3 = this.getSelect3(answer);
                row["A3"] = list3[0];
                row["B3"] = list3[1];
                row["C3"] = list3[2];
                row["D3"] = list3[3];
                //row 添加 第4题
                answer = qaRow["steel_name_en"].ToString();
                if (answer == null)
                {
                    answer = "无";
                }
                else if (answer == "")
                {
                    answer = "无";
                }
                row["answer4"] = answer;
                if (answers.Contains("$"))
                {
                    answers.Replace("$", "#");
                }
                answers += answer + "$";
                List<string> list4 = this.getSelect4(answer);
                row["A4"] = list4[0];
                row["B4"] = list4[1];
                row["C4"] = list4[2];
                row["D4"] = list4[3];
                //row 添加 第5题
                answer = qaRow["usefulness"].ToString();
                if (answer == null)
                {
                    answer = "无";
                }
                else if (answer == "")
                {
                    answer = "无";
                }
                row["answer5"] = answer;
                if (answers.Contains("$"))
                {
                    answers.Replace("$", "#");
                }
                answers += answer;
                List<string> list5 = this.getSelect1(answer);
                row["A5"] = list5[0];
                row["B5"] = list5[1];
                row["C5"] = list5[2];
                row["D5"] = list5[3];
                row["answer"] = answers;
                //添加新题
                table.Rows.Add(row);
            }
            return table;
        }
        /// <summary>
        /// 返回正确的题目和答案
        /// </summary>
        /// <param name="idList"></param>
        /// <returns></returns>
        private List<DataRow> getQAList( DataTable dt, List<int> idList)
        {
            List<DataRow> list = new List<DataRow>();
            for (int i = 0; i < idList.Count; i++)
            {
                list.Add(dt.Rows[idList[i]]);
            }
            return list;
        }
        /// <summary>
        /// 返回第一道题的四个选项，随机排列后的
        /// </summary>
        /// <param name="answer"></param>
        /// <returns></returns>
        private List<string> getSelect1(string rightAnswer)
        {
            string[] answerCollection = new string[] {  "高碳铬轴承钢","碳素结构钢","合金结构钢",
                                            "结构钢","锅炉用钢板","高速工具钢棒","不锈钢棒"};
            return this.getAnswerList(rightAnswer, answerCollection);
        }
        /// <summary>
        /// 返回第二道题的四个选项，随机排列后的
        /// </summary>
        /// <param name="answer"></param>
        /// <returns></returns>
        private List<string> getSelect2(string rightAnswer)
        {
            string[] answerCollection = new string[] {  "高碳铬轴承钢","碳素结构钢","合金结构钢",
                                            "结构钢","锅炉用钢板","高速工具钢棒","不锈钢棒"};
            return this.getAnswerList(rightAnswer, answerCollection);
        }
        /// <summary>
        /// 返回第三道题的四个选项，随机排列后的
        /// </summary>
        /// <param name="answer"></param>
        /// <returns></returns>
        private List<string> getSelect3(string rightAnswer)
        {
            string[] answerCollection = new string[] {  "高碳铬轴承钢","碳素结构钢","合金结构钢",
                                            "结构钢","锅炉用钢板","高速工具钢棒","不锈钢棒"};
            return this.getAnswerList(rightAnswer, answerCollection);
        }
        /// <summary>
        /// 返回第四道题的四个选项，随机排列后的
        /// </summary>
        /// <param name="answer"></param>
        /// <returns></returns>
        private List<string> getSelect4(string rightAnswer)
        {
            string[] answerCollection = new string[] {  "高碳铬轴承钢","碳素结构钢","合金结构钢",
                                            "结构钢","锅炉用钢板","高速工具钢棒","不锈钢棒"};
            return this.getAnswerList(rightAnswer, answerCollection);
        }
        /// <summary>
        /// 返回第五道题的四个选项，随机排列后的
        /// </summary>
        /// <param name="answer"></param>
        /// <returns></returns>
        private List<string> getSelect5(string rightAnswer)
        {
            string[] answerCollection = new string[] {  "高碳铬轴承钢","碳素结构钢","合金结构钢",
                                            "结构钢","锅炉用钢板","高速工具钢棒","不锈钢棒"};
            return this.getAnswerList(rightAnswer, answerCollection);
        }

        private List<string> getAnswerList(string rightAnswer, string[] answerCollection)
        {
            List<string> list = new List<string>();
            if (rightAnswer == "无")
            {
                list.Add("无"); list.Add("无"); list.Add("无"); list.Add("无");
                return list;
            }
            Random rd = new Random();
            int rightNumber = rd.Next(4);
            int total = answerCollection.Length;
            for (int i = 0; i < 3; i++)
            {
                string wrongAnswer = string.Empty;
                do
                {
                    int n = rd.Next(total);
                    wrongAnswer = answerCollection[n];
                }
                while (list.Contains(wrongAnswer) || wrongAnswer == rightAnswer);
                list.Add(wrongAnswer);
            }
            list.Insert(rightNumber, rightAnswer);
            return list;
        }
    }
}
